//
//  NestCollectionViewCell.m
//  TodatPlay
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "NestCollectionViewCell.h"
#import "TAMacro.h"
#import "NestSmallCollectionViewCell.h"
#import "FeaturedTwoAdvlist.h"
#import "FeaturedTwoProjlist.h"
#import "UIImageView+WebCache.h"
#define kNestCollectionViewReuse @"NestReuse"
@interface NestCollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *nestCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *nestFlowLayout;
@property (nonatomic, copy) DidSelectItemAtIndexPath index;
@end

@implementation NestCollectionViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    [self.nestCollectionView registerNib:[UINib nibWithNibName:@"NestSmallCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kNestCollectionViewReuse];
    self.nestCollectionView.bounces = NO;
    self.nestCollectionView.showsHorizontalScrollIndicator = NO;
    self.nestCollectionView.delegate = self;
    self.nestCollectionView.dataSource = self;
    self.nestFlowLayout.minimumLineSpacing = 10;
    self.nestFlowLayout.minimumInteritemSpacing = 10;
    self.nestFlowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.nestFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // Initialization code
}
- (void)setAdvList:(FeaturedTwoAdvlist *)advList {
    _advList = advList;
    [self.nestCollectionView reloadData];
}
#pragma mark - 实现block协议
- (void)didSelectItemAtIndexPath:(DidSelectItemAtIndexPath)index {
    self.index = index;
}
#pragma mark - collectionView的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.index(indexPath);
}


#pragma mark - TableView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.advList.projlist.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NestSmallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kNestCollectionViewReuse forIndexPath:indexPath];
    NSURL *imageURL = [NSURL URLWithString:self.advList.projlist[indexPath.row].pic];
    [cell.picImageView sd_setImageWithURL:imageURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    // 处理字符串
    NSString *pricestrlabel = self.advList.projlist[indexPath.row].pricestr;
    if ([pricestrlabel containsString:@"-"]) {
        NSArray *arr = [pricestrlabel componentsSeparatedByString:@"-"];
        pricestrlabel = nil;
        pricestrlabel = arr.firstObject;
       
    } else if ([pricestrlabel containsString:@"元"]) {
        pricestrlabel =[[pricestrlabel componentsSeparatedByString:@"元"]firstObject];
        
    }
    // 处理字符串
    NSString *discountStr = self.advList.projlist[indexPath.row].discount;
    if ([discountStr containsString:@"-"]) {
        NSArray *arr = [pricestrlabel componentsSeparatedByString:@"-"];
        discountStr = nil;
        discountStr = [NSString stringWithFormat:@"%@元", arr.firstObject];
    }
    // label字体加横线
    NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", discountStr]];
    [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
    
    cell.titleLabel.text = self.advList.projlist[indexPath.row].title;
    cell.pricestrLabel.text = pricestrlabel;
    cell.discountLabel.attributedText = newPrice;
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (SCREEN_WIDTH - 40) / 2.5;
    CGFloat height = self.contentView.bounds.size.height - 10;
    return CGSizeMake(width, height);
}


@end
